package mall

import (
	"context"
	"fmt"
	"github.com/samber/lo"
	"private-domain-overseas-service/app/business/api/internal/repository/mall"
	"private-domain-overseas-service/pkg/constant"
	"private-domain-overseas-service/pkg/status/bizmall"
	"private-domain-overseas-service/pkg/xerr"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzmallsite"
	"private-domain-overseas-service/tools/ent/predicate"

	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/app/business/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MallSiteListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 站点选择下拉框
func NewMallSiteListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MallSiteListLogic {
	return &MallSiteListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MallSiteListLogic) MallSiteList() (resp *types.MallBrandSiteListResponse, err error) {
	siteDao := mall.NewMallSiteDao(l.ctx, l.svcCtx)
	//构建站点查询条件
	sitesConditions := make([]predicate.HzMallSite, 0)
	sitesConditions = append(sitesConditions, hzmallsite.MallSiteShowEQ(bizmall.IsShow), hzmallsite.DeleteAtIsNil())
	//查询数据
	sites, err := siteDao.MallSiteConditionsAll(sitesConditions)
	if err != nil {
		return nil, xerr.ErrCodeMsg(xerr.ServerDataNotFoundError, fmt.Sprintf("查询站点列表失败：%v", err))
	}
	//组装返回数据
	brandSites := lo.Map(sites, func(item *ent.HzMallSite, _ int) *types.MallBrandSite {
		siteIcon := ""
		if item.MallSiteIcon != "" {
			siteIcon = constant.MallGoodsImage + item.MallSiteIcon
		}
		return &types.MallBrandSite{
			SiteId:       int64(item.ID),
			MallSiteName: item.MallSiteName,
			MallSiteIcon: siteIcon,
			MallSiteSort: item.MallSiteSort,
		}
	})

	return &types.MallBrandSiteListResponse{
		List: brandSites,
	}, nil
}
